package com.test1;

import cn.hutool.core.util.XmlUtil;
import com.entity.CC;
import com.entity.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;

public class Te {

    public static void main(String[] args) {
        /*D d = new D();
        Class aClass = null;
        D dd = null;
        try {
            aClass = Class.forName("com.test1.D");


            dd = (D) aClass.newInstance();


            D.class.getSimpleName();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        dd.a();
        System.out.println(aClass);*/

//        readObjectFromXml

       /* try {
            CC cc = new CC();
            cc.setKey("123");
            cc.setClientid("145942");

            Dat dat = new Dat();

            dat.setCod("4566");

            Dat[] dats = {dat};
            cc.setData(dats);
            XmlUtil.writeObjectAsXml(new File("E://a.xml"), cc);

            String ss = XmlUtil.readObjectFromXml("E://a.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
*/
/*
        CC cc = new CC();
        cc.setKey("123");
        cc.setClientid("145942");

        Dat dat = new Dat();

        dat.setCod("4566");

        Dat[] dats = {dat};
        cc.setData(dats);*/

       CC cc1 =(CC)toObj(CC.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "  <void property=\"clientid\">\n" +
                "   <string>145942</string>\n" +
                "  </void>\n" +
                "  <void property=\"data\">\n" +
                "   <array class=\"com.entity.Dat\" length=\"1\">\n" +
                "    <void index=\"0\">\n" +
                "     <object class=\"com.entity.Dat\">\n" +
                "      <void property=\"cod\">\n" +
                "       <string>4566</string>\n" +
                "      </void>\n" +
                "     </object>\n" +
                "    </void>\n" +
                "   </array>\n" +
                "  </void>\n" +
                "  <void property=\"key\">\n" +
                "   <string>123</string>\n" +
                "  </void>\n"
               );

        System.out.println(cc1);

    }


    public static   Object toObj(Class clazz,String xmlStr){
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;

    }
}
